package com.kkwrite.regimen.provider.util;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by Ron on 2018/8/17 10:59.
 *
 * @author Ron
 */
@Component("tokenUtil")
public class TokenUtil {

    private static final String TOKEN_STRING = "token";
    private static final int TOKEN_EXPIRE_MIN_VALUE = 5;

    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;

    public void catchLoginStatus(String token, String phoneNum, String username,
                                 String employeeId, String shopId) {
        Assert.hasText(token, "token must not be null or empty");
        Assert.hasText(phoneNum, "phoneNum must not be null or empty");
        Assert.hasText(username, "username must not be null or empty");
        Assert.hasText(employeeId, "employeeId must not be null or empty");
        Assert.hasText(shopId, "shopId must not be null or empty");

        // employeeId-token 缓存14天
        redisTemplate.opsForHash().put(employeeId, "token", token);
        redisTemplate.expire(employeeId, 14, TimeUnit.DAYS);
        // token-* 缓存15天
        Map<String, String> map = new HashMap<>(16);
        map.put("phoneNum", phoneNum);
        map.put("username", username);
        map.put("employeeId", employeeId);
        map.put("shopId", shopId);
        redisTemplate.opsForHash().putAll(token, map);
        redisTemplate.expire(token, 15L, TimeUnit.DAYS);
    }

    public void updateToken(String token, String phoneNum, String username,
                            String employeeId, String shopId) {
        Assert.hasText(token, "token must not be null or empty");
        Assert.hasText(phoneNum, "phoneNum must not be null or empty");
        Assert.hasText(username, "username must not be null or empty");
        Assert.hasText(employeeId, "employeeId must not be null or empty");
        Assert.hasText(shopId, "shopId must not be null or empty");

        redisTemplate.expire(employeeId, 14L, TimeUnit.DAYS);
        redisTemplate.expire(token, 15L, TimeUnit.DAYS);

        // 更新值
        Map<String, String> map = new HashMap<>(16);
        map.put("phoneNum", phoneNum);
        map.put("username", username);
        map.put("shopId", shopId);
        redisTemplate.opsForHash().putAll(token, map);
    }

    public String existToken(String key) {
        Assert.hasText(key, "key must not be null or empty");

        if (redisTemplate.opsForHash().hasKey(key, TOKEN_STRING)) {
            long expire = redisTemplate.getExpire(key);
            if (expire > TOKEN_EXPIRE_MIN_VALUE) {
                String token = (String) redisTemplate.opsForHash().get(key, "token");
                // 延长14d
                redisTemplate.expire(key, 14, TimeUnit.DAYS);
                // 延长有效期15d
                redisTemplate.expire(token, 15, TimeUnit.DAYS);
                return token;
            }
        }
        return null;
    }

    public Object getValue(String token, TokenKeyEnum key) {
        Assert.hasText(token, "token must not be null or empty");
        Assert.notNull(key, "key must not be null");
        return redisTemplate.opsForHash().get(token, key.getKey());
    }

    public enum TokenKeyEnum {
        /**
         * 手机号
         */
        PHONE_NUM("phoneNum"),
        /**
         * 登录员工用户名
         */
        USERNAME("username"),
        /**
         * 登录员工编号
         */
        EMPLOYEE_ID("employeeId"),
        /**
         * 员工所属店铺ID
         */
        SHOP_ID("shopId");

        private String key;

        TokenKeyEnum(String key) {
            this.key = key;
        }

        public String getKey() {
            return key;
        }
    }

}
